<?php
namespace Admin\Controller;
use Think\Controller;

class CouponsController extends HomeController {

	/*
	* Function:		couponsList
	* Description:	优惠券列表
	*/
   public function couponsList(){
      $filter = I('param.');
      $page =  I('get.p',1);
      $pageSize = 10;
      $filter['flag'] = 1;
      $filter = returnFilter($filter);
      $list = D('Coupons')->getCouponsList($page,$filter,$pageSize);
      $count =  D('Coupons')->getCouponsCount($filter);
      $pages = getpage($count,$pageSize,$filter);
      $show = $pages->show();
      $this->assign('list',$list);
      $this->assign('page',$show);
      $this->assign('filter',$filter);
      $this->display();
   }
    
	/*
	* Function:		addCoupons
	* Description:	添加优惠券
	*/
	public function addCoupons(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			$data['addtime'] = date('Y-m-d H:i:s',time());
			if(D('Coupons')->addCoupons($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->display();
		}
	}
	
	/*
	* Function:		editCoupons
	* Description:	编辑优惠券
	* @param:		$id int id
	*/
	public function editCoupons($id = ''){
		if(IS_POST){
			$data = I('post.');
			$ret = D('Coupons')->updateCoupons($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('Coupons')->getCouponsInfo($id);
				$this->assign('info',$info);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		deleteCoupons
	* Description:	删除优惠券
	* @param:		$id
	*/
	public function deleteCoupons($id = ''){
      $ret = M('coupons')->where(['id'=>$id])->save(['status'=>0]);
		if($ret!==false){
			$ret = array('code'=>1,'message'=>'操作成功');
			$this->ajaxReturn($ret);
		}else{
			$ret = array('code'=>0,'message'=>'操作失败!');
			$this->ajaxReturn($ret);
		}
	}
}